package xh.util;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/6/26.
 */
public class MusicStore {

    public List<String> allMusicNames() {
        File[] files = allMusic();

        List<String> musicList = new ArrayList<String>();
        if (files != null) {
            for (File f : files) {
                musicList.add(f.getName());
            }
        }

        return musicList;
    }

    public List<File> allMusicFiles() {
        File[] files = allMusic();

        if (files == null) {
            return Collections.emptyList();
        } else {
            return Arrays.asList(files);
        }
    }

    public List<URL> allMusicUrls() {
        File[] files = allMusic();

        List<URL> musicList = new ArrayList<URL>();
        if (files != null) {
            for (File f : files) {
                try {
                    musicList.add(f.toURI().toURL());
                } catch (MalformedURLException e) {
                }
            }
        } else {
            return Collections.emptyList();
        }

        return musicList;
    }


    private File[] allMusic() {
        File dir = Global.MusicDir;

        File[] files = dir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                int p = name.lastIndexOf(".");
                if (p < 0) {
                    return false;
                }

                return Global.MusicSuffix.contains(name.substring(p + 1));
            }
        });

        return files;
    }

    public List<MP3FileInfo> allMusicFileInfos() {
        File[] files = allMusic();

        List<MP3FileInfo> musicList = new ArrayList<MP3FileInfo>();
        if (files != null) {
            for (File f : files) {
                MP3FileInfo info = new MP3FileInfo();
                info.setName(f.getName());

                try {
                    MP3File mf = (MP3File) AudioFileIO.read(f);
                    MP3AudioHeader audioHeader = (MP3AudioHeader)mf.getAudioHeader();
                    info.setTimeLengthInMinuteFormat(audioHeader.getTrackLengthAsString());
                    info.setTimeLength(audioHeader.getTrackLength());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                musicList.add(info);
            }
        }

        return musicList;
    }

    public static MP3FileInfo mp3FileInfo(File file) {
        MP3FileInfo info = null;
        try {
            info = new MP3FileInfo();
            MP3File mf = (MP3File) AudioFileIO.read(file);
            MP3AudioHeader audioHeader = (MP3AudioHeader)mf.getAudioHeader();
            info.setTimeLengthInMinuteFormat(audioHeader.getTrackLengthAsString());
            info.setTimeLength(audioHeader.getTrackLength());
            info.setName(file.getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static class MP3FileInfo {
        private String name;
        private int timeLength;
        private String timeLengthInMinuteFormat;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getTimeLength() {
            return timeLength;
        }

        public void setTimeLength(int timeLength) {
            this.timeLength = timeLength;
        }

        public String getTimeLengthInMinuteFormat() {
            return timeLengthInMinuteFormat;
        }

        public void setTimeLengthInMinuteFormat(String timeLengthInMinuteFormat) {
            this.timeLengthInMinuteFormat = timeLengthInMinuteFormat;
        }
    }
}
